package com.andy.beef.main.fragment

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.andy.beef.R
import com.andy.beef.databinding.MainFragmentWorkbenchBinding
import com.andy.beef.main.viewmodel.WorkbenchViewModel
import com.andy.beef.ui.main.adapter.CategoryMenuAdapter
import com.andy.beef.ui.main.bean.MenuItemBean
import com.andy.module.base.BaseVbFragment
import com.andy.module.constant.RoutePath
import com.andy.module.widget.MenuGridDecoration
import com.blankj.utilcode.util.ConvertUtils

/**
 * @creator:LuoFan
 * @date:2022/9/30
 * @description:
 */
class WorkbenchFragment : BaseVbFragment<WorkbenchViewModel, MainFragmentWorkbenchBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        val menuAdapter = CategoryMenuAdapter()
        menuAdapter.setOnItemClickListener { _, _, position ->
            val bean = menuAdapter.getItem(position)
            if (bean.path.isNotEmpty()) {
                ARouter.getInstance().build(bean.path).navigation()
            }
        }
        val manager = GridLayoutManager(requireContext(), 3)
        manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (menuAdapter.getItemViewType(position) == MenuItemBean.VIEW_TYPE_ITEM) 1 else manager.spanCount
            }
        }
        mViewBind.recyclerView.layoutManager = manager
        mViewBind.recyclerView.adapter = menuAdapter
        val itemDecoration = MenuGridDecoration()
        itemDecoration.columnCount = 3
        itemDecoration.horizontalSpace = ConvertUtils.dp2px(2f)
        itemDecoration.verticalSpace = ConvertUtils.dp2px(2f)
        mViewBind.recyclerView.addItemDecoration(itemDecoration)
        initAdapterData(menuAdapter)
    }

    private fun initAdapterData(adapter: CategoryMenuAdapter) {

        val list = mutableListOf<MenuItemBean>()

        //标题栏,独占一行(3格)
        val cattleGroup = MenuItemBean()
        cattleGroup.viewType = MenuItemBean.VIEW_TYPE_GROUP
        cattleGroup.name = "牛只相关"
        list.add(cattleGroup)

        val enterBean = MenuItemBean()
        enterBean.name = "牛只档案"
        enterBean.img = R.mipmap.main_cattle_enter
        enterBean.path = RoutePath.EDIT_CATTLE
        list.add(enterBean)


        adapter.setList(list)
    }

    private fun addEmptyItem(list: MutableList<MenuItemBean>) {
        val emptyBean = MenuItemBean()
        list.add(emptyBean)
    }
}